package com.ming.easyDevelop.utils;

import com.ming.easyDevelop.exception.BusinessException;
import com.ming.easyDevelop.utils.Interface.BaseResponseCode;
import java.util.HashMap;
import java.util.Map;
/**
 * 通用業務代碼枚舉類
 */
public enum CommonResponseCodeEnum implements BaseResponseCode{
    NOT_FOUND(404,"請求地址不存在"),
    SERVER_ERROR(500,"服務器返回錯誤,請聯繫管理員"),
    TYPE_ERROR(600,"服務器返回錯誤,請聯繫管理員"),
    DUPLICATE_KEY_ERROR(601,"數據庫主鍵衝突"),
    SUCCESS(200,"請求成功"),

    ;
    private Integer code;
    private String message;

    CommonResponseCodeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public Integer getCode() {
        return this.code;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    @Override
    public Exception newException() {
        return newException(new BusinessException(this));
    }

    @Override
    public Exception newException(Throwable t) {
        return (BusinessException) t;
    }

    private static Map<Integer,CommonResponseCodeEnum> codeEnumMap = new HashMap<>();
    static {
        for (CommonResponseCodeEnum value : CommonResponseCodeEnum.values()) {
            codeEnumMap.put(value.getCode(),value);
        }
    }

    public static CommonResponseCodeEnum getEnumByCode(Integer code){
        return codeEnumMap.get(code);
    }
}
